#include "heap.h"
#include <stdio.h>
#include <stdlib.h>

struct NodeStruct
{
    int Priority;
    int Element;
};

struct NodeStruct;
typedef struct NodeStruct *Node;
typedef struct NodeStruct NodeMember;

static int NodeIfNull(ElementType a)
{
    Node EleA = (Node)a;
    return EleA == NULL;
}

static int NodeCompare(ElementType a, ElementType b)
{
    Node EleA = (Node)a;
    Node EleB = (Node)b;
    if (EleA->Priority < EleB->Priority)
        return -1;
    else if (EleA->Priority > EleB->Priority)
        return 1;
    else
        return 0;
}

static int NodeCopy(ElementType *a, ElementType b)
{
    Node *EleA = (Node *)a;
    Node EleB = (Node)b;
    *a = b;
    return 0;
}

static int NodeGenMin(ElementType *minPos, int minPro)
{
    Node *Ele = (Node *)minPos;
    *Ele = malloc(sizeof(**Ele));
    (*Ele)->Priority = minPro;
    return 0;
}

static int NodeReleaseMin(ElementType *minPos)
{
    Node *Ele = (Node *)minPos;
    free(*Ele);
    Ele = NULL;
    return 0;
}

static int NodePrint(ElementType a)
{
    Node EleA = (Node)a;

    printf("ele: %d, pro: %d\n", EleA->Element, EleA->Priority);
    return 0;
}

int main()
{
    int i = 0;
    NodeMember *map = malloc(13 * sizeof(*map));
    for (i = 0; i < 13; i++)
    {
        map[i].Element = i;
    }
    PriorityQueue H = NULL;
    H = Initialize(
        20,
        NodeIfNull,
        NodeCompare,
        NodeCopy,
        NodeGenMin,
        NodeReleaseMin,
        NodePrint);

    map[0].Priority = 31;
    Insert(map, H);
    map[1].Priority = 13;
    Insert(map + 1, H);
    map[2].Priority = 21;
    Insert(map + 2, H);
    map[3].Priority = 24;
    Insert(map + 3, H);
    map[4].Priority = 26;
    Insert(map + 4, H);
    map[5].Priority = 65;
    Insert(map + 5, H);
    map[6].Priority = 32;
    Insert(map + 6, H);
    map[7].Priority = 16;
    Insert(map + 7, H);
    map[8].Priority = 19;
    Insert(map + 8, H);
    map[9].Priority = 68;
    Insert(map + 9, H);
    map[10].Priority = 68;
    Insert(map + 10, H);
    map[11].Priority = 68;
    Insert(map + 11, H);
    map[12].Priority = 68;
    Insert(map + 12, H);
    PrintTree(H);
    DeleteMin(H);
    PrintTree(H);
}
